var express = require('express')
var router = express.Router()
var connection = require('../db/sql.js')
let user = require('../db/userSql.js')

/* GET users listing. */
router.get('/', function (req, res, next) {
  res.send('respond with a resource')
})

//用户登录
router.post('/login', handlerUserLogin).get('/login', handlerUserLogin)
//用户登录
router.post('/login', handlerUserLogin).get('/login', handlerUserLogin)
//用户注册
router.post('/register', handlerUserRegister).get('/register', handlerUserRegister)
//修改密码
router.post('/modify', handlerUserModify).get('/modify', handlerUserModify)

function handlerUserLogin(req, res, next) {
  console.log('/users/login :>> ')
  //前端传给后端的id
  let account = req.query.userAccount
  let password = req.query.userPassword
  if (!account || !password) {
    return res.json({
      code: 300,
      message: '账号或密码为空',
    })
  }
  connection.query(user.queryUserInfo({ userAccount: account }), function (error, result) {
    if (error) {
      console.error('Database query error:', error)
      return result.status(500).send({
        code: 0,
        message: '服务器内部错误',
      })
    }
    if (result[0]?.userPassword === req.query.userPassword) {
      delete result[0].userPassword
      res.json({
        code: 200,
        data: result[0],
        message: '登录成功',
      })
    } else {
      res.json({
        code: 301,
        data: {},
        message: '登录失败,帐号或密码错误',
      })
    }
  })
}

function handlerUserRegister(req, res, next) {
  //前端传给后端的id
  let account = req.body.userAccount
  let password = req.body.userPassword
  if (!account || !password) {
    return res.json({
      code: 300,
      message: '账号或密码为空',
    })
  }
  //判断帐户是否存在
  connection.query(user.queryUserInfo({ userAccount: account }), function (error, result) {
    if (error) throw error
    if (result.length > 0) {
      return res.json({
        code: 303,
        message: '账号已存在',
      })
    }
    connection.query(user.insertNewUser(req.body), function (error, result) {
      if (error) {
        console.error('Database query error:', error)
        return result.status(500).send({
          code: 0,
          message: '服务器内部错误',
        })
      }
      if (result?.insertId > 0) {
        res.json({
          code: 200,
          data: { uid: result.insertId },
          message: '注册成功',
        })
      } else {
        res.json({
          code: 301,
          data: {},
          message: '注册失败',
        })
      }
    })
  })
}

function handlerUserModify(req, res, next) {
  let account = req.body.userAccount
  let password = req.body.userPassword
  if (!account || !password) {
    return res.json({
      code: 300,
      message: '账号或密码为空',
    })
  }
  //判断帐户是否存在
  connection.query(user.queryUserInfo({ userAccount: account }), function (error, result) {
    if (error) throw error
    if (!result.length || result.length <= 0) {
      return res.json({
        code: 304,
        message: '账号不存在',
      })
    }
    connection.query(user.updateUserPassword(req.body), function (error, result) {
      if (error) {
        console.error('Database query error:', error)
        return result.status(500).send({
          code: 0,
          message: '服务器内部错误',
        })
      }
      console.log('object :>> ', result)
      if (result?.affectedRows === 1) {
        res.json({
          code: 200,
          data: { uid: result.insertId },
          message: '修改成功',
        })
      } else {
        res.json({
          code: 301,
          data: {},
          message: '修改失败',
        })
      }
    })
  })
}

module.exports = router
